#pragma comment(lib, "Ws2_32.lib")
#include <iostream>
#include <ShlObj.h>
#include <winsock.h>

//Get Hostname & IP ~By DreammVB~
//Example of returning the local hostname and IP address.

using namespace std;
using std::cout;
using std::endl;

enum THostInfo{
	HOST_NAME = 0,
	HOST_IP = 1
};

string GetLocalHostInfo(THostInfo Opt){
	struct hostent *HostInfo;
	WSADATA WsData;
	char *StrIpAddr = NULL;
	char StrHostName[80];
	string RetVal = "";

	//Start up winsock
	if(WSAStartup(MAKEWORD(1,1),&WsData) != 0){
		return "";
	}
	//Get computer host name
	if(gethostname(StrHostName, sizeof(StrHostName)) == SOCKET_ERROR){
		return "";
	}
	//Get info
	HostInfo = gethostbyname(StrHostName);

	//Get the IP address of local host.
	StrIpAddr = inet_ntoa (*(struct in_addr *)*HostInfo->h_addr_list);

	switch(Opt){
		case HOST_NAME:
			//Return hostname
			RetVal = StrHostName;
			break;
		case HOST_IP:
			//Return IP
			RetVal = StrIpAddr;
			break;
		default:
			break;
	}

	//Clean up
	WSACleanup();
	memset(StrHostName,0,sizeof(StrHostName));
	//Return string
	return RetVal;
}

int main(int argc, char **anvg){
	//Print out local hostname and ip address.
	cout << "Hostname      : " << GetLocalHostInfo(HOST_NAME).c_str() << endl;
	cout << "IP Address    : " << GetLocalHostInfo(HOST_IP).c_str() << endl;

	//Keep console open
	system("pause");
	return 1;
}